package com.huangliusong.spring.servlet;

import com.huangliusong.spring.servlet.AsyncServlet.AsyncServlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import java.util.EnumSet;

/**
 * @author huangliusong
 * @since 2018/10/6
 * {@link EnableAutoConfiguration}
 */
@EnableAutoConfiguration
//@ServletComponentScan(basePackages = "com.huangliusong.spring.servlet")
public class SpringBootServletBootstrap {
    public static void main(String[] args){
        SpringApplication.run(SpringBootServletBootstrap.class,args);

    }

    @Bean
    public ServletContextInitializer servletContextInitializer(){
        return servletContext ->{
            CharacterEncodingFilter filter=new CharacterEncodingFilter();
            FilterRegistration.Dynamic registration= servletContext.addFilter("filter",filter);
            registration.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST),false,"/");
        };
    }

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public ServletRegistrationBean asyncServlet(){
        ServletRegistrationBean registrationBean=new ServletRegistrationBean(new AsyncServlet(),"/-servlets");
        return registrationBean;
    }

}
